clc;
clear all;
close all;

%% INIT
N = 8;
k = 2;
lower = 0.0;
upper = 0.99999999999;

%% Some constants
step = 2^(-N);
P1 = 0:0.05:1;

tcn_ep = zeros(1,length(P1));
cct_ep = zeros(1,length(P1));
vct_ep = zeros(1,length(P1));
hct_ep = zeros(1,length(P1));

ptest = zeros(1, length(P1));

%% Simulator
for i = 1:length(P1)
    fprintf('i = %d\n', i);
    p = P1(i);    
    for a=lower:step:upper
        for b = lower:step:upper            
            pab = prob(a, b, N, N, p);
            ptest(i) = ptest(i) + pab;
            
            ans_true = a * b;
            ans_tcn = trunc_mult(a, b, 0.0, N, N, N, k);
            ans_cct = ccm(a, b, N, N, N, k); 
            ans_vct = vcm(a, b, N, N, N, k);
            ans_hct = hcm(a, b, N, N, N, k, 0.5);
            
            tcn_ep(i) = tcn_ep(i) + abs(ans_tcn - ans_true) * pab;
            cct_ep(i) = cct_ep(i) + abs(ans_cct - ans_true) * pab;
            vct_ep(i) = vct_ep(i) + abs(ans_vct - ans_true) * pab;
            hct_ep(i) = hct_ep(i) + abs(ans_hct - ans_true) * pab;
        end
    end
    
end

figure(1);
plot(P1, tcn_ep, 'r--x'); hold on;
plot(P1, cct_ep, 'g--+'); hold on;
plot(P1, vct_ep, 'b--*'); hold on;
plot(P1, hct_ep, 'm--d'); 
legend('TCN', 'CCT', 'VCT', 'HCT');
